#include <iostream>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>

using namespace std;

int main(){

    //打开
    int fd = open("log.txt", O_RDWR | O_CREAT, 0666);
    if(fd == -1){
        cerr << "open fail!" << endl;
        exit(-1);
    }

    //写入
    char str[] = "i like linux!";
    write(fd, str, sizeof(str));

    //定位到文件开头
    lseek(fd, 0, SEEK_SET);

    //读取
    char buf[64];
    ssize_t ret = read(fd, buf, sizeof(buf) - 1);
    if(ret > 0){
        buf[ret] = '\0';
        cout << buf << endl;
    }

    //关闭
    close(fd);

    return 0;
}